# setwd("~/Dropbox/T&G project survey/Ukraine/replication_JOP")
# libraries
library(tidyverse)
library(countrycode)
# library(gtrendsR)
source("func.R")

# ggplot defaults: geoms theme
update_geom_defaults("text", list(family = "Archivo Narrow"))
update_geom_defaults("label", list(family = "Archivo Narrow"))
theme_set(theme_nice())

## ------------------------------------------------------------
## Google Topics

f_t = list.files("data/gtrends")[!grepl("^search", list.files("data/gtrends"))]

df_list_t = lapply(f_t, function(x){
  d = read.csv(paste0("data/gtrends/", x), skip = 1)
  names(d) = c("date", "hits")
  d$keyword = str_split(x, "_")[[1]][1]
  d$country = gsub("\\.csv", "", str_split(x, "_")[[1]][2])
  d$hits = as.integer(ifelse(d$hits == "<1", 0, d$hits))
  return(d)
})

df_t = do.call(bind_rows, df_list_t) %>%
  mutate(
    topic = recode(keyword,
      "af" = "Armed forces",
      "nw" = "Nuclear weapon",
      "i" = "Inflation",
      "ng" = "Natural gas price",
      "ec" = "Energy crisis"),
    countryn = countrycode(country, "iso2c", "country.name"),
    date = as.Date(date))

# Plot Spain
p = ggplot(df_t %>% filter(country == "ES"), aes(x = date, y = hits)) +
  geom_line() +
  labs(x = "", y = "Search-specific relative interest in topics in Spain") +
  geom_vline(xintercept = as.Date("2022-02-24"), linetype = "dotted") +
  facet_wrap(~topic)

ggsave("figures/gtrends_topics.pdf", width = 7, height = 5, device = cairo_pdf)


# Plot Spain vs Europe

colors = rep("#00000080", length(unique(df_t$country)))
colors[which(unique(df_t$country) == "ES")] = "#F8766D"

p = ggplot(df_t, aes(x = date, y = hits, group = country, color = country)) +
  geom_line() +
  labs(x = "", y = "Search-specific relative interest in topics by country") +
  geom_vline(xintercept = as.Date("2022-02-24"), linetype = "dotted") +
  facet_wrap(~topic, ncol = 1) +
  scale_color_manual(values = colors) +
  theme(legend.position = "none")

ggsave("figures/gtrends_topics_EU.pdf", width = 6, height = 10, device = cairo_pdf)

## ------------------------------------------------------------
## Google Trends - searches

f_s = list.files("data/gtrends")[grepl("^search", list.files("data/gtrends"))]

df_list_s = lapply(f_s, function(x){
  d = read.csv(paste0("data/gtrends/", x), skip = 1)
  names(d) = c("date", "hits")
  d$keyword = gsub("\\.csv", "", str_split(x, "_")[[1]][2])
  d$hits = as.integer(ifelse(d$hits == "<1", 0, d$hits))
  return(d)
})

df_s = do.call(bind_rows, df_list_s) %>%
  mutate(date = as.Date(date)) %>%
  filter(date >= "2022-02-01")

# Plot
gtp = ggplot(df_s, aes(x = date, y = hits)) +
  geom_line() +
  labs(x = "", y = "Search-specific relative interest in Spain") +
  geom_vline(xintercept = as.Date("2022-02-24"), linetype = "dotted") +
  facet_wrap(~keyword)

ggsave("figures/gtrends.pdf", width = 7, height = 5, device = cairo_pdf)




#########################
# Retrieve code - impossible bc of API change

# countries = c("ES", "FR", "IT", "DE", "PL", "RO", "EE", "LV")
# # c("AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI",
# #   "FR", "DE", "GR", "HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL",
# #   "PL", "PT", "RO", "SK", "SI", "ES", "SE")
# topics = c("/m/01h6rj", "/m/05gpf", "/m/09jx2", "/m/0d1k5y", "/m/01ms6r")
#
# comb = expand.grid(c = countries, k = topics) %>%
#   mutate(c = as.character(c), k = as.character(k)) %>%
#   arrange(c)
#
#
# # Set up loop, wait ca 20 min between each query
# for(i in 1:nrow(comb)){
#
#   # Checks
#   current = paste(comb$c[i], comb$k[i])
#   fn = paste0(comb$c[i], gsub("/", "-", comb$k[i]), ".csv")
#   if(fn %in% list.files("data/gtrends")){
#     print(paste0(current, " (already done)"));next
#   } else {print(paste0("Doing: ", current))}
#
#   # Retrieve
#   gt = gtrends(keyword = comb$k[i], time = "2022-01-01 2022-03-30",
#       geo = comb$c[i], gprop = "web")$interest_over_time %>%
#     mutate(topic = recode(keyword,
#       "/m/01h6rj" = "Armed forces",
#       "/m/05gpf" = "Nuclear weapon",
#       "/m/09jx2" = "Inflation",
#       "/m/0d1k5y" = "Natural gas price",
#       "/m/01ms6r" = "Energy crisis"))
#
#   # Save
#   write.csv(gt, paste0("data/gtrends/", fn), row.names = FALSE)
#
#   Sys.sleep(1800)
#
# }
